require("split")


-- wifi.NULLMODE   0
-- wifi.STATION    1
-- wifi.SOFTAP     2
-- wifi.STATIONAP  3
    
wifi.setmode(1)
station_cfg={}
station_cfg.ssid = "HUAWEI_H122_E714"
station_cfg.pwd = "fh123456"
station_cfg.save = false
wifi.sta.config(station_cfg)


scl = 1
sda = 2
i2c.setup(0, sda, scl, i2c.SLOW)
sla = 0x3c
disp = u8g2.ssd1306_i2c_128x64_noname(0,sla)

disp:clearBuffer()
disp:setFont(u8g2.font_6x10_tf)
disp:drawStr(68,64,"For Yozora")
--disp:setFont(u8g2.font_unifont_t_symbols)
disp:drawBox(0,0,128,2)
disp:drawBox(0,62,60,2)
disp:sendBuffer()




--mode timer mode:
--tmr.ALARM_SINGLE a one-shot alarm (and no need to call tobj:unregister())
--tmr.ALARM_SEMI manually repeating alarm (call tobj:start() to restart)
--tmr.ALARM_AUTO automatically repeating alarm

local worker = tmr.create()
worker:alarm(3000, tmr.ALARM_AUTO, function(t)
    print("CHECK")
    if (wifi.sta.getip() ~= nil) then
        ip, nm, gw = wifi.sta.getip()
--        t:unregister()
        t:interval(5*60*1000)
        print(ip)
        disp:drawStr(2,14,ip)
        disp:sendBuffer()
        
        --http://pv.sohu.com/cityjson
        http.get("http://ddns.oray.com/checkip", nil, function(code, data)
            if (code < 0) then
                disp:drawStr(2,28,"HTTP FAIL")
                disp:sendBuffer()
            else
                print(data)
--                local tmp = Util.split(data,"=")[2]
--                tmp=Util.split(tmp,";")[1]
--                t = sjson.decode(tmp)
--                print(t["cip"])
                local tmp = Util.split(data,": ")[2]
                print(tmp)
                disp:drawStr(2,28,tmp)
                disp:sendBuffer()

--http://18672985218:yozora@ddns.oray.com/ph/update?hostname=24hx621469.qicp.vip&myip=27.16.230.119
--http://yozora-hitagi:yozora00@ddns.oray.com/ph/update?hostname=f30712j512.zicp.vip&myip=27.16.230.119
                
--                http.get("http://18672985218:yozora@ddns.oray.com/ph/update?hostname=24hx621469.qicp.vip&myip=27.16.230.119", nil, function(code, data)
--                    if (code < 0) then
--                        disp:drawStr(2,42,"HTTP FAIL")
--                        disp:sendBuffer()
--                    else
--                        print(data)
--                        disp:drawStr(2,42,data)
--                        disp:sendBuffer()
--                    end
--            
--                end)

                
            end
        end)

        
    end
end )
worker:start()